﻿/* Sharepoint Developer's Hub 
 * Author       : Ram Prasad Meenavalli (ram@spdeveloper.co.in)
 * Date         : Jun 30 2012
 * Description  : This class has the utility methods used the custom cmdlets and custom timer jobs 
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Xml;
using CabinetFile;
using Microsoft.SharePoint.Administration;

namespace SPDev.Powershell
{
    /// <summary>
    /// This class contains the static utility methods
    /// </summary>
    internal static class SPDUtility
    {
        const String strTempFolder = "SPDTempFiles";
        /// <summary>
        /// Gets the list of features from the specified solution which are added in the Farm.
        /// </summary>
        /// <param name="spSolution">The SPSolution object which is used for filtering the features from the Farm</param>
        /// <returns>A string collection which has the relative path of the feature</returns>
        internal static List<String> GetFeatures(SPSolution spSolution)
        {
            SPFeatureDefinitionCollection FarmFeatures = SPFarm.Local.FeatureDefinitions;
            var features = from SPFeatureDefinition f in FarmFeatures
                           where f.SolutionId.Equals(spSolution.SolutionId)
                           select (f.DisplayName + "\\feature.xml").ToLower();

            return features.ToList<String>();
        }
        /// <summary>
        /// Gets the list of features which are present in the provided solution package
        /// </summary>
        /// <param name="LiteralPath">The complete path of the new solution package</param>
        /// <returns>A string collection of the Relative Path of the features</returns>
        internal static List<string> GetFeatures(string LiteralPath)
        {
            List<String> features = new List<string>();
            String strTempPath = System.IO.Path.GetTempPath();
            DirectoryInfo tempDirectory = new DirectoryInfo(strTempPath);
            tempDirectory = tempDirectory.CreateSubdirectory(strTempFolder);
            strTempPath = tempDirectory.FullName;            
            TCabinetFile pkgFile = new TCabinetFile();
            pkgFile.Name = LiteralPath;
            pkgFile.OutputDirectory = strTempPath;
            pkgFile.ExtractAll();
            XmlDocument manifestFile = new XmlDocument();
            manifestFile.Load(strTempPath + "\\manifest.xml");
            XmlNodeList featureNodes = manifestFile.GetElementsByTagName("FeatureManifest");
            foreach (XmlNode featureNode in featureNodes)
            {
                features.Add(featureNode.Attributes["Location"].Value.ToLower());
            }
            tempDirectory.Delete(true);
            return features;
        }
        /// <summary>
        /// Check if the Administration Service is running
        /// </summary>
        /// <returns>A boolean which specifies whether the administration service is running or not</returns>
        internal static bool IsAdministrationServiceRunning()
        {
            bool result;
            using (ServiceController serviceController = new ServiceController("SPAdminV4"))
            {
                result = (serviceController.Status == ServiceControllerStatus.Running);
            }
            return result;
        }
        /// <summary>
        /// Splits the given string using ';#' as seperator and gives the list of feature URLs
        /// </summary>
        /// <param name="features">A string representing the list of features seperated by ';#'</param>
        /// <returns>A collection of feature paths</returns>
        internal static string[] GetSplitFeatures(string features)
        {
            String[] FeatureLocs = features.Split(new String[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);
            return FeatureLocs;
        }
        /// <summary>
        /// Merges the given feature urls using ';#' as the seperator
        /// </summary>
        /// <param name="features">Collection of features</param>
        /// <returns>A string which has the list of features which are seperated by ';#'</returns>
        internal static String MergerFeatureNames(IEnumerable<string> features)
        {
            StringBuilder featureNames= new StringBuilder();
            foreach (var f in features)
            {
                featureNames.AppendFormat("{0}{1}", f, ";#");
            }
            return featureNames.ToString();
        }
    }
}